/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.gui.impl;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;

import com.rh.game.Character;
import com.rh.game.EventsAndChat;
import com.rh.game.World;

import com.rh.game.chat.Chat;
import com.rh.game.chat.ChatImage;
import com.rh.game.chat.ChatResponse;

import com.rh.game.impl.DemoGame;

import com.rh.gui.Gui;

/**
 *
 * @author Hayden
 */
public class ConversationJDialog extends JDialog {
    public static void main(String[] args)  {
        System.out.println("ConversationJDialog");
        World w = DemoGame.getGameWorld();
        Gui g = new Gui1();
        System.out.println(chat(g, w, "Chat3"));
    }
    
    private static void debug(Object o)  {
        // System.out.println("ConversationJDialog " + o);
    }
    
    private Chat chat = null;
    final JList listResponses = new JList();
    private String selection = null;
    
    private ConversationJDialog(Chat pChat)  {
        super();
        chat = pChat;
        this.setModal(true);
        initComponents();
    }
    
    private void exit()  {
        setVisible(false);
        dispose();
    }
    
    private void select(final int index)  {
        Object o = listResponses.getModel().getElementAt(index);
        ChatResponse cr = (ChatResponse) o;
        selection = cr.getID();
        exit();
    }
    
    private void initComponents()  {
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        this.setTitle("Conversation");
        if (chat.getSpeaker() == Character.NARRATOR)  this.setTitle("Narration");
        
        setLayout(new BorderLayout());

        JTextPane paneText = new JTextPane();
        paneText.setMinimumSize(new Dimension(50, 50));
        paneText.setPreferredSize(new Dimension(300, 100));
        paneText.setEditable(false);
        paneText.setText(chat.getText());
        getContentPane().add(paneText, BorderLayout.CENTER);
        
        JComponent south = null;
        
        if (chat.getResponses().size() > 0)  {
            DefaultListModel dlm = new DefaultListModel();
            for (ChatResponse cr : chat.getResponses())  {
                dlm.addElement(cr);
            }
            listResponses.setModel(dlm);
            MouseListener mouseListener = new MouseAdapter() {
                 public void mouseClicked(MouseEvent e) {
                     if (e.getClickCount() == 2) {
                         select(listResponses.locationToIndex(e.getPoint()));
                      }
                 }
            };
            listResponses.addMouseListener(mouseListener);
            south = listResponses;
        }
        else  {
            JButton ok = new JButton("OK");
            ok.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent ae)  {
                        exit();
                    }
                }
            );
            south = ok;
        }
        
        getContentPane().add(south, BorderLayout.SOUTH);
        
        ChatImage ci = chat.getImage();
        if (ci == ChatImage.NULL)  ci = chat.getSpeaker().getImage();
        
        ImageJPanel imageHolder = new ImageJPanel(ci);
        this.add(imageHolder, BorderLayout.EAST);        
    }
    
    public static boolean chat(final Gui gui, final World world, String pChatID)  {
        EventsAndChat ec = world.getChat(pChatID);
        while (ec != EventsAndChat.NULL)  {
            boolean gameOver = gui.render(ec.getEvents(), world);
            if (gameOver)  return true;
            
            // Now do the Chat
            Chat c = ec.getChat();
            if (c == Chat.NULL)  break;
            
            ConversationJDialog cjp = new ConversationJDialog(c);
            cjp.pack();
            cjp.setVisible(true);

            String resp = cjp.selection;
            if (resp == null)  break;
            
            String nextChatID = null;
            for (ChatResponse cr : c.getResponses())  {
                if (cr.getID().equals(resp))  {
                    nextChatID = cr.getNextChatID();
                }
            }

            if (nextChatID == null)  {
                debug("Repsonse not recognized!");
            }
            else  {
                ec = world.getChat(nextChatID);
            }            
        }
        
        return false;
    }
    
    private boolean chat2(final Gui gui, final World world, String pChatID)  {
        EventsAndChat ec = world.getChat(pChatID);
        debug(pChatID + " ==> " + ec);

        while (ec != EventsAndChat.NULL)  {
            // Render the Events
            boolean gameOver = gui.render(ec.getEvents(), world);
            if (gameOver)  return true;
            
            // Now do the Chat
            Chat c = ec.getChat();
            if (c == Chat.NULL)  break;
            
            String message = c.getText();
            String responses = "";
            for (ChatResponse cr : c.getResponses())  {
                responses = responses + "\n(" + cr.getID() + ") " + cr.getText();
            }

            String resp = JOptionPane.showInputDialog(null, message + responses, "Chat with " + c.getSpeaker().getName(), JOptionPane.PLAIN_MESSAGE);

            if ((resp == null) || (c.getResponses().size() == 0))  {
                break;
            }            

            String nextChatID = null;
            for (ChatResponse cr : c.getResponses())  {
                if (cr.getID().equals(resp))  {
                    nextChatID = cr.getNextChatID();
                }
            }

            if (nextChatID == null)  {
                debug("Repsonse not recognized!");
            }
            else  {
                ec = world.getChat(nextChatID);
            }
        }
        
        return false;
    }    
}
